import 'package:flutter/cupertino.dart';
import 'package:pixelyear/bean/DateBean.dart';
import 'package:pixelyear/bean/YearBean.dart';
import 'package:pixelyear/utils/constant/YxsColor.dart';
import 'package:pixelyear/utils/extension/CanvasUtils.dart';
import 'package:pixelyear/utils/extension/ContextUtils.dart';
import 'package:pixelyear/utils/extension/ObjectUtils.dart';
import 'package:flutter/material.dart';
import 'package:pixelyear/utils/Utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PixelView {
  static Widget builder(BuildContext context, YearBean data,
      Function(int year, int month, int day) click) {
    int week;
    int year = data.year;
    int month = data.month;
    int days;
    int columnSize;
    var boxHeight = 50.0.w; //高度
    var boxWidth = 50.0.w; //宽度
    var boxPadding = 1.0.w; //分割线宽度

    void calcData() {
      week = Utils.dateTranslateWeek(year, month, 01) - 1;
      if (month == 1 ||
          month == 3 ||
          month == 5 ||
          month == 7 ||
          month == 8 ||
          month == 10 ||
          month == 12) {
        days = 31;
      } else if (month == 2) {
        if ((year / 100 != 0 && year / 4 == 0) ||
            (year / 100 == 0 && year / 400 == 0)) {
          days = 29;
        } else
          days = 28;
      } else {
        days = 30;
      }
      if ((days + week) % 7 == 0) {
        columnSize = (days + week) ~/ 7;
      } else {
        columnSize = ((days + week) / 7 + 1).toInt();
      }
    }

    calcData();
    var viewHeight = columnSize * (boxHeight + boxPadding) + boxPadding;
    return GestureDetector(
      onTapDown: (TapDownDetails details) {
        double mX = details.localPosition.dx;
        double mY = details.localPosition.dy;

        int w = mX ~/ boxWidth;
        int h = mY ~/ boxHeight;
        int clickDay = (h) * 7 + w + 1 - week;
        click(year, month, clickDay);
      },
      child: CustomPaint(
        isComplex: true,
        size: Size(context.windowWidth, viewHeight),
        painter: _PixelView(context, data, boxWidth, boxHeight, boxPadding,
            days, week, columnSize),
      ),
    );
  }
}

class _PixelView extends CustomPainter {
  BuildContext mContext;
  YearBean mData;
  double boxWidth; //宽度
  double boxHeight; //高度
  double boxPadding; //分割线宽度
  int columnSize; //行数
  var rowSize = 7; //列数
  var month = 0; //需要画的月份
  var year = 0; //需要画的年份
  var nowMonth = 0; //现在年份
  var nowYear = 0; //现在月份
  var nowDay = 0; //现在日期 天
  int days;
  int week; //星期 用来判断每月一次需要空几格
  Color lastColor = Colors.white;
  Color taskTextColor = Colors.white;
  var nowDayAttr = []; //今字的宽高
  var marginLR = 5.w; //左右间距
  var marginTB = 5.w; //左右间距

  //正常日期画笔
  var boxPaint = Paint()
    ..color = Colors.white
    ..isAntiAlias = true;

  //空白日期画笔
  var emptyPaint = Paint()
    ..color = Color(0xfff7f7f7)
    ..isAntiAlias = true;

  //背景颜色画笔
  var backPaint = Paint()
    ..color = YxsColor.blackf3f4f5
    ..isAntiAlias = true;

  _PixelView(BuildContext context, YearBean data, double width, double height,
      double padding, int days, int week, int columns) {
    mContext = context;
    this.mData = data;
    month = mData.month;
    year = mData.year;
    nowMonth = Utils.getMonth();
    nowYear = Utils.getYear();
    nowDay = Utils.getDay();
    this.boxWidth = width;
    this.boxHeight = height;
    this.boxPadding = padding;
    this.days = days;
    this.week = week;
    this.columnSize = columns;
    initTextWidth();
  }

  @override
  void paint(Canvas canvas, Size size) {
    _onDraw(canvas, size);
  }

  void _onDraw(Canvas canvas, Size size) {
    //画背景
    canvas.drawRect(
        Rect.fromLTRB(
            marginLR,
            0,
            rowSize * (boxWidth + boxPadding) + marginLR + boxPadding,
            columnSize * (boxHeight + boxPadding) + boxPadding),
        backPaint);

    for (int i = 0; i < columnSize; i++) {
      for (int j = 0; j < rowSize; j++) {
        var thisDay = (i * rowSize) + j + 1;
        if (thisDay - week <= days && thisDay > week) {
          Iterable<DateBean> filter =
              mData.list.where((element) => element.day == (thisDay - week));

          if (filter.isNotEmpty) {
            if (lastColor.value != filter.first.color ||
                taskTextColor.value != filter.first.textColor) {
              lastColor = Color(filter.first.color);
              boxPaint.color = lastColor;
              taskTextColor = Color(filter.first.textColor);
            }
            //画有标记的日期
            canvas.drawRect(
                Rect.fromLTRB(
                    (boxWidth * j) + boxPadding * (j + 1) + marginLR,
                    (boxHeight * i) + boxPadding * (i + 1),
                    (boxWidth * (j + 1)) + boxPadding * (j + 1) + marginLR,
                    (boxHeight * (i + 1)) + boxPadding * (i + 1)),
                boxPaint);

            canvas.drawCenterText(
                boxWidth,
                "${thisDay - week}",
                TextStyle(fontSize: 15, color: taskTextColor),
                (boxWidth * j) + boxPadding * (j + 1) + marginLR,
                ((boxHeight) * (i + 1)) + boxPadding * (i + 1));

            //画心情
            canvas.drawCenterIcon(
                boxWidth,
                filter.first.mood,
                (boxWidth * j) + boxPadding * (j + 1) + marginLR,
                ((boxHeight) * (i + 1)) + boxPadding * (i + 1),
                12.sp,
                taskTextColor);

            //画有任务标记
            if (filter.first.note != null && filter.first.note.isNotEmpty) {
              canvas.drawPaintIcon(
                  boxWidth,
                  (boxWidth * j) + boxPadding * (j + 1) + marginLR,
                  ((boxHeight) * (i + 1)) + boxPadding * (i + 1),
                  12.sp,
                  taskTextColor);
            }
          } else {
            if (lastColor != Colors.white ||
                taskTextColor != YxsColor.black999) {
              lastColor = Colors.white;
              boxPaint.color = lastColor;
              taskTextColor = YxsColor.black999;
            }

            canvas.drawRect(
                Rect.fromLTRB(
                    (boxWidth * j) + boxPadding * (j + 1) + marginLR,
                    (boxHeight * i) + boxPadding * (i + 1),
                    (boxWidth * (j + 1)) + boxPadding * (j + 1) + marginLR,
                    (boxHeight * (i + 1)) + boxPadding * (i + 1)),
                boxPaint);

            canvas.drawCenterText(
                boxWidth,
                "${thisDay - week}",
                TextStyle(fontSize: 15, color: taskTextColor),
                (boxWidth * j) + boxPadding * (j + 1) + marginLR,
                ((boxHeight) * (i + 1)) + boxPadding * (i + 1));
          }
        } else {
          //画没有字的块
          canvas.drawRect(
              Rect.fromLTRB(
                  (boxWidth * j) + boxPadding * (j + 1) + marginLR,
                  (boxHeight * i) + boxPadding * (i + 1),
                  (boxWidth * (j + 1)) + boxPadding * (j + 1) + marginLR,
                  (boxHeight * (i + 1)) + boxPadding * (i + 1)),
              emptyPaint);
        }

        if (year == nowYear &&
            month == nowMonth &&
            (thisDay - week) == nowDay) {
          canvas.drawText(
            "今",
            TextStyle(fontSize: 7.sp, color: taskTextColor),
            (boxWidth * j) + boxPadding * (j + 1) + marginLR * 2,
            ((boxHeight) * i) + boxPadding * (i + 1) + marginTB,
          );
        }
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  } //画笔的宽度

  void initTextWidth() {
    nowDayAttr = drawTextWidthAndHeight("今", TextStyle(fontSize: 7));
    //计算今字的上左间距
    // nowDayHeight = (nowDayRect.height() * 1.5).toInt()
    // nowDayWidth = nowDayRect.width()

    // moodPaint.getTextBounds(paint, 0, paint.length, moodRect)
    // moodTextWidth = moodRect.width()

    boxWidth = boxHeight =
        (mContext.windowWidth - (boxPadding * (rowSize + 1)) - (marginLR * 2)) /
            rowSize;
  }
}
